<?php

namespace app\admin\model\express;

use think\Model;


class Package extends Model
{
    // 表名
    protected $name = 'express_package';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'state_text',
        'status_text',
        'is_open_text',
        'is_delivery_text'
    ];



    public function getStateList()
    {
        return [  '1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3'), '4' => __('State 4'),'5' => __('State 5'),'6' => __('State 6'),'7' => __('State 7'),'8' => __('State 8')];
    }
    public function getStatusList()
    {
        return [
            'waitpackage' => __('Status waitpackage'),  //待打包
            'batch' => __('Status batch'),  //已分配批次
            'delivered' => __('Status delivered'),   //已发货
            'arrived' => __('Status arrived'),        //已抵达
            'waitingpay' => __('Status waitingpay'),  //待支付
            'successpay' => __('Status successpay'),  //支付成功
        ];
    }

    public function getPayStatusList(){
        return [
            0=>'未支付',1=>'支付待审核',2=>'审核通过',3=>'未通过'
        ];
    }


    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getIsDeliveryList()
    {
        return ['0' => __('Is_delivery 0'), '1' => __('Is_delivery 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDeliveryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_delivery']) ? $data['is_delivery'] : '');
        $list = $this->getIsDeliveryList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function order(){
        return $this->hasMany('app\admin\model\express\Order','package_id','id');
    }

//    public function goods(){
//        return $this->hasMany('app\admin\model\express\OrderGoods','order_id','id');
//    }


    public function setOrderNoAttr($value,$data){
        return 'TY'.substr(str_shuffle(substr(time(),-8).substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).$value,-8)),0,6);
    }
}
